perf(payments): add indexes for payments list filters - #6341
Draft
sarkissianraffi wants to merge 1 commit into
Draft
sarkissianraffi wants to merge 1 commit into
sarkissianraffi wants to merge 1 commit into
Conversation
Supports the payments list filters for large organizations: - payment_receipts (organization_id, lower(number)) for receipt_number - invoices (organization_id, lower(number)) for invoice_number - payments (organization_id, payable_payment_status, created_at DESC, id) partial on pending/processing for the rare payment_status values All builds are concurrent. To be deployed before the filters change.
This was referenced Sep 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds the indexes the payments list filters (#6325) rely on for large organizations. Migration-only PR: no application code. Deploy this before #6325.
Indexes
index_payment_receipts_on_organization_id_lower_numberon(organization_id, lower(number))receipt_numberfilterindex_invoices_on_organization_id_lower_numberon(organization_id, lower(number))invoice_numberfilter (both payable paths)index_payments_on_org_pending_processing_created_aton(organization_id, payable_payment_status, created_at DESC, id) WHERE payable_payment_status IN ('pending', 'processing')payment_statusvalues, pre-sorted for the list. Partial on purpose: the planner cannot pick it forsucceeded/failed, which the cursor index already servesSame shape as
index_invoices_on_organization_id_lower_purchase_order_number. All builds usealgorithm: :concurrently,if_not_exists: true, one migration per table.Build cost, synthetic dataset
Measured on a synthetic dataset of ~6.5M payments / 5.9M invoices / 3.5M receipts (see
script/perf/payments_filters/on the filters branch). Figures are from that dataset, not from production.Rejected after measurement (details in the internal doc): a full
(organization_id, payable_payment_status, created_at DESC, id)index (423 MB, the planner takes it forsucceededand the count regresses),(organization_id, amount_cents)(same regression on common ranges), and an expression index on the payment method jsonb (never used while the saved-method fallback branch exists).Deploying
config/database.ymlappliesLAGO_DATABASE_STATEMENT_TIMEOUTto every session, includingdb:migrate. Run the migration with the statement timeout unset (or above the expected build time). A shortLAGO_DATABASE_LOCK_TIMEOUTis fine:CREATE INDEX CONCURRENTLYtakes aShareUpdateExclusiveLockonly.INVALIDindex behind. Check after the deploy and drop/retry if any row comes back:DROP INDEX CONCURRENTLY <name>, no application change needed.SELECT indexrelname, idx_scan FROM pg_stat_user_indexes WHERE indexrelname LIKE '%lower_number%'.Performance analysis: internal document "Payments list filters: performance analysis" (Raffi; not public). All figures above come from the synthetic dataset built by
script/perf/payments_filters/generate.rbon #6325.